package com.heima.article.v1.controller;

import com.heima.common.dtos.ResponseResult;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import com.heima.user.entity.FollowConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class ArticleBehaviorCollection {

    @Autowired
    private StringRedisTemplate redisTemplate;
    public  static  final String COLLECT="COLLECT";

    /**
     * 文章收藏
     */
    @PostMapping("/api/v1/collection_behavior")
    public ResponseResult behavior(@RequestBody CollectionBehaviorDto dto) {
        //获取用户ID
        Integer userId = UserThreadLocalUtils.getUserId();
        //获取文章、动态ID
        Long entryId = dto.getEntryId();

        String Kid = "" + entryId;

        Integer operation = dto.getOperation();
        //添加数据
        //判断用户的状态
        if (operation==0){
            //如果为0，表示用户进行关注操作,将其存储到缓存中
            redisTemplate.opsForHash().put(Kid,COLLECT, FollowConstant.FOLLOW_OPERATION_CANCEL);
        }else {
            //如果不为0，表示用户进行取消关注操作,方法将其存储到缓存中
            redisTemplate.opsForHash().put(Kid,COLLECT, FollowConstant.FOLLOW_OPERATION_SUCCESS);

        }



        return ResponseResult.ok();
    }


}
